package com.epoch.client.view;


import com.epoch.client.kernel.listener.LoginListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClientLoginJFrame extends JFrame implements ActionListener {

    //定义北部
    JLabel labTop;

    //定义中部
    JPanel Panel_2;
    JTextField txtNum;
    JPasswordField passWord;
    JButton pan2_butNum,pan2_butPass;
    JLabel pan2_labNum,pan2_labPass;
    JTabbedPane tabPane;
    //定音南部
    JPanel Panel_1;
    JButton pan1_butLog,pan1_butRes;

    public ClientLoginJFrame(){
        //处理北部
        labTop = new JLabel(new ImageIcon(this.getClass().getResource("/imgs/top_label.png")));
        this.add(labTop,"North");

        //处理中部
        Font fontCenter = new Font("华文行楷", 0, 18);
        Panel_2 = new JPanel(new GridLayout(2,3));
        Panel_2.setBackground(Color.WHITE);
        pan2_labNum = new JLabel("用 户 名 称：",JLabel.CENTER);
        pan2_labPass = new JLabel("用 户 密 码：",JLabel.CENTER);
        pan2_butNum = new JButton("清 除 名 称");
        pan2_butPass = new JButton("清 除 密 码");
        pan2_labPass.setFont(fontCenter);
        pan2_labNum.setFont(fontCenter);
        pan2_butNum.setFont(fontCenter);
        pan2_butPass.setFont(fontCenter);
        pan2_butPass.setBackground(new Color(150,200,230));
        pan2_butNum.setBackground(new Color(150,200,230));
        txtNum = new JTextField();
        passWord = new JPasswordField();
        passWord.setBackground(Color.lightGray);

        //将部件加入
        Panel_2.add(pan2_labNum);
        Panel_2.add(txtNum);
        Panel_2.add(pan2_butNum);
        Panel_2.add(pan2_labPass);
        Panel_2.add(passWord);
        Panel_2.add(pan2_butPass);

        //创建选项卡窗口
        tabPane = new JTabbedPane();
        tabPane.add("账户名称登录",Panel_2);
        this.add(tabPane,"Center");
        tabPane.setBackground(Color.cyan);
        
        //处理南部
        Panel_1 = new JPanel();
        pan1_butLog = new JButton("登 录");
        pan1_butRes = new JButton("注 册");
        pan1_butLog.setFont(new Font("华文行楷",0,16));
        pan1_butRes.setFont(new Font("华文行楷",0,16));
        pan1_butLog.setBackground(new Color(180,220,230));
        pan1_butRes.setBackground(new Color(180,220,230));

        Panel_1.add(pan1_butLog);
        Panel_1.add(pan1_butRes);
        Panel_1.setBackground(new Color(193,210,240));
        this.add(Panel_1,"South");


        //添加按钮监听器
        pan2_butPass.addActionListener(this);
        pan2_butNum.addActionListener(this);
        pan1_butLog.addActionListener(new LoginListener(txtNum,passWord,this));
        pan1_butRes.addActionListener(this);

        
        this.setSize(600,300);
        Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension fra = this.getSize();
        int width = (int) (scr.getWidth()-fra.getWidth())/2;
        int height = (int) (scr.getHeight() - fra.getHeight())/2;
        this.setLocation(width,height);
        this.setIconImage(new ImageIcon(this.getClass().getResource("/imgs/login_title.png")).getImage());
        this.setTitle("E·C·H·O");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == pan2_butNum){
            txtNum.setText("");
        }
        else if(e.getSource() == pan2_butPass){
            passWord.setText("");
        }
        else if(e.getSource() == pan1_butRes){
            ClientRegisterJFrame resJFrame = new ClientRegisterJFrame();
            this.dispose();
        }
    }
}